package com.sangu.dianxiu.ui;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.sangu.dianxiu.R;

public class LocActivity extends BaseActivity implements BDLocationListener {//实现百度地图定位监听接口

    private LocationClient locationClient;
    @ViewInject(R.id.iv_activity_loc_back)  //使用XUtils注解，这样就不用再findViewById了
    private ImageView ivBack;
    @ViewInject(R.id.tv_activity_loc_currentcity)
    private TextView tvCurrentCity;
    private boolean isLocSuccess=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loc);
        ViewUtils.inject(this);     //调用此方法完成注解
        initLocation();
    }
    private void initLocation()
    {
        locationClient=new LocationClient(this,getLocOption());
        locationClient.registerLocationListener(this);
        locationClient.start();     //启动定位
    }
    private LocationClientOption getLocOption() {
        LocationClientOption option=new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType("bd0911");
        option.setScanSpan(3 * 1000);
        option.setIsNeedAddress(true);
        option.setOpenGps(true);
        return option;
    }
    //使用XUtils的点击事件注解，这样就不用setOnClickListener了
    @OnClick({R.id.iv_activity_loc_back,R.id.tv_activity_loc_currentcity})
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.iv_activity_loc_back:
                finish();
                break;
            case R.id.tv_activity_loc_currentcity:
                Intent intent = new Intent();
                if(isLocSuccess)
                {
                    intent.putExtra("currentCity", tvCurrentCity.getText().toString());
                    setResult(1, intent);
                    finish();
                }
            case R.id.iv_activity_lock_icon:
                locationClient.requestLocation();
                break;
        }
    }

    //定位的回调方法，bdLocation中包含着定位信息
    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        String city = bdLocation.getCity();
        if(city!=null)
        {
            isLocSuccess=true;
            tvCurrentCity.setText(city);
            locationClient.stop();  //当定位成功后停止定位
        }
        else
        {
            tvCurrentCity.setText("定位失败");
        }

    }
}
